
//
//  OrderDetailViewController.m
//  EasyToTesco
//
//  Created by mc on 16/2/23.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "HeaderFileForEasyToTesco.h"
//引入自定义的cell
#import "HomeTableViewCell.h"
#import "OrderDetailTableViewCell.h"
//订单支付头文件
#import "OrderPayViewController.h"
//编辑收货地址头文件
#import "EditAddressViewController.h"
//#
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"

@interface OrderDetailViewController ()<UITableViewDataSource, UITableViewDelegate>{
    NSString *cellCount;
    NSString *price;
}

@property (nonatomic, strong)UITableView *OrderDetailTableview;


@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self addorderDetailInit];
    [self addOrderDetailTopView];
    [self addOrderDetailtableview];
    [self addOrderDetailUI];
    
    [self orderDetaildatasources];
}

//用于初始化
- (void)addorderDetailInit {
    cellCount = @"5";
    price = @"686";
}
//添加头部导航视图
- (void)addOrderDetailTopView {
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"订单详情";

}
- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//添加tableview
- (void)addOrderDetailtableview {
    self.OrderDetailTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.83) style:UITableViewStylePlain];
    self.OrderDetailTableview.delegate = self;
    self.OrderDetailTableview.dataSource = self;
    self.OrderDetailTableview.tableFooterView =[[UIView alloc] init];
    self.OrderDetailTableview.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
    [self.view addSubview:self.OrderDetailTableview];
}
//添加UI
- (void)addOrderDetailUI {
    
    //底层的view
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, HIGHT*0.93, (WEITH/3)*2, HIGHT*0.068)];
    firstView.backgroundColor = [UIColor whiteColor];
    firstView.layer.borderWidth = .6;
    firstView.layer.borderColor = [UIColor grayColor].CGColor;//[UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1].CGColor;
    [self.view addSubview:firstView];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake((WEITH/3)*2, HIGHT*0.93, WEITH/3, HIGHT*0.068);
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    
    UILabel *translabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(firstView.frame)-60, 5, 60, CGRectGetHeight(firstView.frame)-10)];
    translabel.text = @"不含运费";
    translabel.font = [UIFont systemFontOfSize:11];
    [firstView addSubview:translabel];
    
    UILabel *Allprice = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(translabel.frame)-80, 5, 80, CGRectGetHeight(firstView.frame)-10)];
    Allprice.textColor = [UIColor redColor];
    Allprice.text = [NSString stringWithFormat:@"¥%.2f", [price floatValue]];
    Allprice.textAlignment = NSTextAlignmentLeft;
    Allprice.font = [UIFont systemFontOfSize:12.5];
    [firstView addSubview:Allprice];
    
    UILabel *otherlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(Allprice.frame)-60, 5, 60, CGRectGetHeight(firstView.frame)-10)];
    otherlabel.text = @"合计:  ";
    otherlabel.textAlignment = NSTextAlignmentRight;
    otherlabel.font = [UIFont systemFontOfSize:12.5];
    [firstView addSubview:otherlabel];
    
}

- (void)sureBtnClick{
    OrderPayViewController *orderpayVC = [[OrderPayViewController alloc] init];
    [self presentViewController:orderpayVC animated:YES completion:nil];
    
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0||section == 1) {
        return 1;
    }else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        static NSString *identifier = @"HomeTableViewCell";
        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [self setLeftViewWithTextField:cell.location imageName:@"Icon_-Location-pin.png"];
            [self setLeftViewWithTextField:cell.NumOfBuy imageName:@"men-gray-icon.png"];
        }
        return cell;
    }else if (indexPath.section == 0){
        static NSString *identifier = @"OrderDetailTableViewCell";
        OrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"OrderDetailTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        return cell;

    }else {
        return nil;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 8;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 70;
    }else if(indexPath.section == 1) {
        return 80;
    }else {
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0||section == 1) {
        return 10;
    } else {
        return 30;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WEITH, 30)];
    footView.backgroundColor = [UIColor whiteColor];
    NSArray *footTitlearr = @[@"收货时间:",@"活动:",@"快递方式:", @"收货备注:", [NSString stringWithFormat:@"共%@件商品", cellCount], [NSString stringWithFormat:@"积分(共%@积分, 可用%@积分)", cellCount, cellCount]];
    if (section == 0||section == 1) {
        footView.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
    } else if (section >=2){
        UILabel *titlebel = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 80, 20)];
        titlebel.text =footTitlearr[section - 2];
        titlebel.font = [UIFont systemFontOfSize:14];
        [footView addSubview:titlebel];
        if (section == 6) {
            footView.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
//            UILabel *otherLabel = [[UILabel alloc] initWithFrame:CGRectMake(WEITH-170, 5, 50, 20)];
//            otherLabel.text = @"合计:";
//            otherLabel.font = [UIFont systemFontOfSize:14];
//            [footView addSubview:otherLabel];
            UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(WEITH - 88, 5, 80, 20)];
            priceLabel.textColor = [UIColor redColor];
            priceLabel.font = [UIFont systemFontOfSize:14];
            priceLabel.text = [NSString stringWithFormat:@"¥%.2f", [price floatValue]];
           
            [footView addSubview:priceLabel];
            
            UILabel *otherLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(priceLabel.frame)-50, 5, 50, 20)];
            otherLabel.text = @"合计:  ";
            
            otherLabel.textAlignment = NSTextAlignmentRight;
            otherLabel.font = [UIFont systemFontOfSize:14];
            [footView addSubview:otherLabel];
        }
        if (section == 7) {
            titlebel.frame = CGRectMake(8, 5, 200, 20);
        }
    }
    
    footView.layer.borderWidth = 0.5;
    footView.layer.borderColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1].CGColor;
    return footView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        EditAddressViewController *editAddressVC = [[EditAddressViewController alloc] init];
        editAddressVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self presentViewController:editAddressVC animated:YES completion:nil];
    }
}

/**
 *  给UITextField设置右侧的图片
 *
 *  @param textField UITextField
 *  @param imageName 图片名称
 */

-(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    
    rightView.image = [UIImage imageNamed:imageName];
    rightView.frame = CGRectMake(0, 0, CGRectGetHeight(textField.frame), CGRectGetHeight(textField.frame));
    rightView.contentMode = UIViewContentModeCenter;
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}

- (void)orderDetaildatasources {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *orderid = self.orderDetailId;
    

    NSLog(@"%@", orderid);
    NSDictionary *param = @{@"orderid":[NSString stringWithFormat:@"%@", orderid]};
    
    [mgr POST:@"http://www.ylgw8.com/AppOrd/Getordinfo" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
       
        NSLog(@"%@",string);
        
        [self.OrderDetailTableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
//         NSLog(@"数据请求错误原因:%@", error);
//         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}


// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
